﻿#include "usrinfo.h"

UsrInfo::UsrInfo()
{

}

//对数据库进行增删改查
//插入数据
void  UsrInfo::InsertUsrInfoDataToDb(QSqlDatabase db,User usr)
{
// 构造sql语句
    QString str = QString("insert into usrLoginTable values('%1','%2')").arg(usr.usrName,usr.passWd);
    qDebug()<< str;
    ExecuteResults results =this->my_op.DbExecute(db, str) ;
    if(results.flag)
    {
        qDebug()<<"sucess";

    }
    else
    {
        qDebug()<<"error";
    }

}

// 查询所有的记录
QSqlTableModel* UsrInfo::SelectUsrInfosFromModel(QSqlDatabase db)
{
    model = new QSqlTableModel(0,db); //
    model->setTable("usrloginTable");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();

    return model;

}
// 查询数据表中所有记录
QList<User> UsrInfo::SelectUsrInfos(QSqlDatabase db)
{
    QList<User> usrInfos;

    // 构造sql语句
        QString str = QString("select * from usrlogin");
        qDebug()<< str;
        ExecuteResults results = this->my_op.DbExecute(db, str) ;
        if(results.flag)
        {
            qDebug()<<"sucess";
            while(results.query.next())
            {
                qDebug() << results.query.value(0).toString() <<" "
                         << results.query.value(1).toString()<<" "
                         <<results.query.value(2).toString();
                User usr;
                usr.usrName = results.query.value(1).toString();
                usr.passWd = results.query.value(2).toString();
                usrInfos.append(usr);
            }

        }
        else
        {
            qDebug()<<"error";
        }
        return usrInfos;
}
